package com.lanou.beautifulwardrobe.brand.adapter;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.activity.BrandShopActivity;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.EditRecommendBrand;
import com.lanou.beautifulwardrobe.brand.EditDialogFragment;

import java.util.List;


/**
 * Created by dllo on 15/10/28.
 */
public class EditGridViewAdapter extends BaseAdapter {


    private List<EditRecommendBrand> datas;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private int position;
    private ViewGroup parent;
    private ImageLoader imageLoader;

    //    public void addData(List<EditRecommendBrand> editRecommendBrands) {
//        datas = editRecommendBrands;
//        notifyDataSetChanged();
//    }
    public void addData(List<EditRecommendBrand> datas) {
        this.datas = datas;
        notifyDataSetChanged();
        ;
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View view, final ViewGroup parent) {
        this.parent = parent;
        this.position = position;
        Holder holder;
        if (view == null) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.brand_edit_item, null);

            holder = new Holder();
            holder.brandFirstImage = (NetworkImageView) view.findViewById(R.id.brandFirstImage);
            holder.brandSecondImage = (NetworkImageView) view.findViewById(R.id.brandSecondImage);
            holder.brandThirdImage = (NetworkImageView) view.findViewById(R.id.brandThirdImage);
            holder.brandLogoImage = (NetworkImageView) view.findViewById(R.id.brandLogoImage);
            holder.brandLikes = (TextView) view.findViewById(R.id.brandLikes);
            holder.brandDescription = (TextView) view.findViewById(R.id.brandDescription);
            holder.brandStyle = (TextView) view.findViewById(R.id.brandStyle);

            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }

        if (datas != null && datas.size() > 0) {
            EditRecommendBrand brand = datas.get(position);

            holder.brandDescription.setText(brand.getDescription());
            holder.brandLikes.setText(brand.getLikes_count());
            holder.brandStyle.setText(brand.getStyle());
            getBitMap(brand.getLogo_url(), holder.brandLogoImage);
            List<String> imageUrls = brand.getTaobao_pic_urls();
            if (imageUrls != null) {
                for (int i = 0; i < imageUrls.size(); i++) {
                    switch (i) {
                        case 0:
                            getBitMap(imageUrls.get(i), holder.brandFirstImage);
                            break;
                        case 1:
                            getBitMap(imageUrls.get(i), holder.brandSecondImage);
                            break;
                        default:
                            getBitMap(imageUrls.get(i), holder.brandThirdImage);
                            break;
                    }
                }
            }
            //holder.brandFirstImage.setOnClickListener(this);
        }

        holder.brandFirstImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditDialogFragment dialogFragment = new EditDialogFragment(datas.get(position).getProducts(),0);
                FragmentActivity fa = (FragmentActivity) parent.getContext();
                dialogFragment.show(fa.getSupportFragmentManager(), "dialogFragment");
            }
        });
        holder.brandSecondImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditDialogFragment dialogFragment = new EditDialogFragment(datas.get(position).getProducts(),1);
                FragmentActivity fa = (FragmentActivity) parent.getContext();
                dialogFragment.show(fa.getSupportFragmentManager(), "dialogFragment");
            }
        });
        holder.brandThirdImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditDialogFragment dialogFragment = new EditDialogFragment(datas.get(position).getProducts(),2);
                FragmentActivity fa = (FragmentActivity) parent.getContext();
                dialogFragment.show(fa.getSupportFragmentManager(), "dialogFragment");
            }
        });
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(parent.getContext(), BrandShopActivity.class);
                long id= datas.get(position).getId();
                String imageUrl=datas.get(position).getLogo_url();
                String title=datas.get(position).getTitle();
                intent.putExtra("id",id+"");
                intent.putExtra("title",title);
                intent.putExtra("imageUrl",imageUrl);
                parent.getContext().startActivity(intent);
               // Toast.makeText(parent.getContext(), (position + 1) + "", Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }

    private ViewPager dialogViewpager;




    private class Holder {
        private NetworkImageView brandFirstImage, brandSecondImage, brandThirdImage, brandLogoImage;
        private TextView brandLikes, brandDescription, brandStyle;

    }

    /** 拉取图片 **/
    private void getBitMap(String url, NetworkImageView imageView){
        imageLoader = volleySingleton.getImageLoader();
        imageView.setDefaultImageResId(R.mipmap.ic_launcher);
        imageView.setErrorImageResId(R.mipmap.ic_launcher);
        imageView.setImageUrl(url, imageLoader);
    }

//    private void method(ViewGroup parent,final List<EditRecommendProduct> product) {
//        EditDialogFragment dialogFragment = new EditDialogFragment(product);
//        FragmentActivity fa = (FragmentActivity) parent.getContext();
//        dialogFragment.show(fa.getSupportFragmentManager(), "dialogFragment");
//    }
//    private void parse() {
//        volleySingleton.getStringRequset(Contants.BRAND_EDIT_URL, new Response.Listener<String>() {
//            @Override
//            public void onResponse(String response) {
//                Gson gson = new Gson();
//                EditRecommendResult editRecommendResult = gson.fromJson(response, EditRecommendResult.class);
//                if (editRecommendResult.getMsg().equals("ok")) {
//                    datas = editRecommendResult.getData().get(0).getBrands();
//                    notifyDataSetChanged();
//                }
//            }
//        });
//    }
}
